#ifndef __ENGINE_REQUEST_included
#define __ENGINE_REQUEST_included

#include <boost/lexical_cast.hpp>
#include "package.h"
#include "Response.h"

using namespace boost;
using namespace std;

#define COMMAND_REGISTER "Register"
#define COMMAND_CHECK "Check"
#define COMMAND_SYNC "Sync"
#define COMMAND_CONNECT "Connect"
#define COMMAND_SETUP "Setup"
#define COMMAND_GET "Get"

namespace Engine {

class CDevice;

class CRequest : public CPackage
{
	bool mEnsureSend;

protected :

	string mCommand;
	std::map<string, string> mParameters;
	
protected :
	
	virtual bool ParseParameters(string &value);
	void PostParse();

public :

	CRequest(void);
	CRequest(string& cmd);
	CRequest(string& cmd, boost::asio::streambuf& data);
	CRequest(string& cmd, boost::shared_ptr<std::istream> is);
	virtual ~CRequest(void);

	bool IsEnsureSend() { return mEnsureSend; }
	void setEnsureSend(bool b) { mEnsureSend = b; }
	string& GetCommand() { return mCommand; }
	void SetCommand(string& cmd) { mCommand = cmd; }
	void AddParameter(string& name, string& value) { mParameters.insert(pair<string, string>(name, value)); }
	string& GetParameter(string& name) {
		map<string, string>::iterator i = mParameters.find(name);
		if(i != mParameters.end()) return i->second;
		else return CPackage::NULL_STRING;
	}

	int GetParameterInt(string& name) {
		string& s = GetParameter(name);
		if(s.length() == 0) return 0;
		try {
			return boost::lexical_cast<int>(s);
		}
		catch(std::exception&) {
		}

		return 0;
	}

	void UpdateMagic();
	ResponseRef InitResponse();

	virtual void GetData(ostream& os);
	virtual bool IsRequest() { return true; }
	virtual void DoRequest() { };
	virtual void HandleResponse(ResponseRef response) { };
	virtual void Response(int status);
};

typedef boost::shared_ptr<CRequest> RequestRef;
}

#endif //__ENGINE_REQUEST_included